<?php

namespace app\common\model;

//use think\Model;

use Exception;

class UserModel extends BaseModel {

	protected $table = 'tp_user';
	protected $readonly = ['username','phone'];

	/*
	获取用户登录密码
	 */
	public function getPassword($id) {
		$data = $this->where('id', $id)->value('password');
		return $data;
	}

    /*
    获取用户交易密码
     */
    public function getPaypass($id) {
        $data = $this->where('id', $id)->value('paypass');
        return $data;
    }

    /*
    密码除外
     */
	public function getUserInfo($id){
		$data = $this->where('id', $id)->field('password,paypass',true)->find();
        //$data = $data->toArray();
		return $data;
	}

    /*
    全部
     */
    public function getAllUserInfo($id){
        $data = $this->where('id', $id)->find();
        return $data;
    }

    /*
    用户名获取数据
     */
    public function infoByUsername($username){
        $data = $this->where('username',$username)->find();
        return $data;
    }

    /*
    设备号获取
     */
    public function infoByRid($rid){
        $data = $this->where('rid',$rid)->count();
        return $data;
    }
    /*
    电话号码获取数据
     */
    public function infoByPhone($phone){
        $data = $this->where('phone',$phone)->find();
        return $data;
    }
    /*
    邮箱获取数据
     */
    public function infoByMail($mail){
        $data = $this->where('mail',$mail)->find();
        return $data;
    }
    /*
    邀请码获取数据pid
     */
    public function idByInviteCode($invite_code){
        $data = $this->where('invite_code',$invite_code)->field('id,quhao,phone')->find();
        return $data;
    }

    /*
    更新用户信息
     */
    public function updateUser($id,$data){
        if(empty($id) || empty($data)) {
            return false;
        }
        $res = $this->where('id', $id)->update($data);
        return $res;
    } 

    /*
    电话号码计数
     */
    public function countPhone($phone){
        $count = $this->where('phone',$phone)->count();
        return $count;
    }

    /*
    添加用户
     */
    public function addNewUser($data){
        $invite_code = $this->setInviteCode($data['phone']);
        if(!$invite_code){
            return false;
        } 
        $data = [
            'username'        =>$data['username'],
            'quhao'           =>$data['quhao'],
            'phone'           =>$data['phone'],
            'password'        =>md5($data['password']),
            'paypass'         =>$data['paypass'],
            'pid'             =>$data['pid'],
            'jdid'            =>$data['jdid'],
            'position'        =>$data['position'],
            'status'          =>1,
            'mail'            =>$data['mail'], 
            'avatar'          =>$data['avatar'],
            'rid'             =>$data['rid'],
            'identity_status' =>0,
            // 'is_active'       =>0,
            'invite_code'     =>$invite_code,
            'date'            =>$this->getTime(), 
            'reg_date'        =>$this->getDate()
        ];
        $res = $this->insertGetId($data);
        return $res;
    }

    /*
    获取邀请码
     */
    public function setInviteCode($phone){
        $str = md5($phone.uniqid().microtime().rand(1,9));
        $invite_code = substr($str, 0,6);
        $res = $this->where('invite_code',$invite_code)->find();
        if($res){
            $this->setInviteCode($phone);
        } else {
            return $invite_code;
        }
    }


    /*
    获取全部下级
     */
    public function getAllXid($uid){
        $data = [];
        $uids = [];
        $i = 0;
        while ($uid) {
            $uid = $this->where('pid','in',$uid)->column('id');
            if ($uid) {
                $uids = array_merge($uids,$uid);
                $i += 1;
            }
        }
        $data['uids']   = $uids;
        $data['depth'] = $i;
        return $uids;
    }
    /*
    获取全部节点id
     */
    public function getAllJdid($uid){
        $data = [];
        $uids = [];
        $i = 0;
        while ($uid) {
            $uid = $this->where('jdid','in',$uid)->column('id');
            if ($uid) {
                $uids = array_merge($uids,$uid);
                $i += 1;
            }
        }
        $data['uids']   = $uids;
        $data['depth'] = $i;
        return $uids;
    }
     /*
    指定代之内uid
    */
    public function getAssigPid($uid,$depth){
        $uids = [];
        for ($i=1; $i < $depth+1; $i++) { 
            $uid = $this->where('pid','in',$uid)->column('id');
            if ($uid) {
                $uids = array_merge($uid,$uids);
            } else {
                break;
            }
        }
        return $uids;
    }

    /*
    获取指定代级uid
    */
    public function getOnePid($uid,$depth=1){
        $layer = 1;
        while ($layer <= $depth) {
            $uid = $this->where('pid','in',$uid)->column('id');
            if (empty($uid)) {
                break;
            }
            $layer += 1;
        }
        return $uid;
    }
}
